/*
  Copyright 1996-2003
  Simon Whiteside

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  $Id: XMLElementObjectEnumerator.java,v 1.1 2003/05/06 11:52:24 simkin_cvs Exp $
*/
package simkin;
import java.util.*;
import org.kxml.kdom.*;
import org.kxml.*;
import org.kxml.io.*;
import org.kxml.parser.*;

/**
 * This class enumerates the elements in an XMLElementObject 
 * 
 *
 */
public class XMLElementObjectEnumerator implements ExecutableIterator{
    XMLElementObject m_ElementObject;
    String m_TagName="";
    int m_NodeNum=0;
    /**
     * Constructor - enumerates over every child node of this object
     */
    public XMLElementObjectEnumerator(XMLElementObject elementObject){
	m_ElementObject=elementObject;
	// first wind forward to the first occurrence of the tag
	findNextNode();
    }
    /**
     * Constructor - enumerates over children of this object with the matching tag
     */
    public XMLElementObjectEnumerator(XMLElementObject elementObject,String tagName){
	m_ElementObject=elementObject;
	m_TagName=tagName;
	// first wind forward to the first occurrence of the tag
	findNextNode();
    }
    /**
     * This method resets the enumeration to the beginning - you can now iterate over the list again
     */
    public void reset(){
	m_NodeNum=0;
	findNextNode();
    }
    /**
     * returns the next object in the enumeration - or null if there are no more
     */
    public Object next(){
	XMLElementObject ret=null;
        int num_nodes=m_ElementObject.getElement().getChildCount();
	if (m_NodeNum<num_nodes){
	    Element elem=(Element)m_ElementObject.getElement().getChild(m_NodeNum);
	    String name=elem.getName();
	    ret=m_ElementObject.createXMLElementObject(Utils.addStrings(m_ElementObject.getLocation(),".",name),elem);
	    m_NodeNum++;
	    findNextNode();
	}
	return ret;
    }
    /**
     * this method winds m_NodeNum onto the next matching element - or beyond the end of the list for the end of enumeration
     */
    void findNextNode(){
        int num_nodes=m_ElementObject.getElement().getChildCount();
        for (;m_NodeNum<num_nodes;m_NodeNum++){
            int type=m_ElementObject.getElement().getType(m_NodeNum);
            if (type==Xml.ELEMENT){
                Element node=(Element)m_ElementObject.getElement().getChild(m_NodeNum);
                if (m_TagName.length()==0 || node.getName().equals(m_TagName)){
                    break;
                }
            }
        }
    }
}
